<html>
    <head>
        <title>DoReMon</title>
        <link href="public/css/css.css" rel="stylesheet" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      
    </head>
    <body>
            <?php
            require_once './model/works.php';
            $keyword = new Works();
            $keyword->table_name = "works";
            ?>
            <?php
            if (!isset($_GET["page"]))
                $page = 1;
            else
                $page = $_GET["page"];
            $total = $keyword->countWords();
            $work = $keyword->selects(null, $_GET['id']);
            ?>
            <div class="tong">
<div class="menu">
        <ul>
            <li><a href="index.php"> TRANG CHỦ</a></li>
           <li><a href="#">GIỚI THIỆU</a></li>
            <li><a href="#">SẢN PHẨM</a>
               <ul>
                 <li><a href="#">APPLE</a></li>
                 <li><a href="#">DELL</a></li>
                 <li><a href="#">SAMSUNG</a></li>
                 <li><a href="#">PANASONIC</a></li>
               </ul>
           </li>   
           
          <li><a href="#">THIẾT BỊ MẠNG</a>
               <ul>
                 <li><a href="#">USB 3G</a></li>
                 <li><a href="#">HUB-SWICHT</a></li>
                 <li><a href="#">CABLE MẠNG</a></li>
                 <li><a href="#">KHÁC</a></li>
               </ul>
           </li>
            <li><a href="#"> THIẾT BỊ VĂN PHÒNG</a>
                <ul>
                 <li><a href="#">MÁY IN</a></li>
                 <li><a href="#">MÁY SCAN</a></li>
                 <li><a href="#">MÁY FAX</a></li>
                 <li><a href="#">MÁY TÍNH TIỀN</a></li>
               </ul>
            </li>
            <li><a href="#">LIÊN HỆ</a></li>
        </ul>
    
        </div>
                
                  <img src="public/image/a.jpg"/>
                    
            

                <div id="boder"></div>
                    
                      
                        <div  class="edit">
                            <form method="POST" action="index_edit.php?id=<?php echo $_GET['id']?>" enctype="multipart/form-data">
                                <table>
                                    <tr>
                                        <td><label for="file">id:</label></td> 
                                 <td><input type="text" name="id" value="<?php echo $work['id']; ?>"></td>
                                 </tr>
                                 
                                 <tr>
                                     <td><label for="file">name:</label></td>
                                    <td><input type="text" name="name" value="<?php echo $work['name']; ?>"></td>
                                 </tr>
                                <tr>
                                <td><label for="file">location</label></td>
                                <td><input type="text" name="location" value="<?php echo $work['location']; ?>"></td>
                                </tr>
                                <tr>
                                <td><label for="file">company_address:</label></td>
                               <td> <input type="text" name="company_address" id="company_address"value="<?php echo $work['company_address']; ?>"></td>
                                </tr>
                                <tr>
                                <td><label for="file">images:</label></td>
                                <td><input type="file" name="file" id="file"></td>
                                </tr>
                                </table>
                                <input type="submit" name="submit" value="Submit" style="margin-left: 200px ; margin-top: 50px">
                            </form></div>
                            <?php
                            if (isset($_POST['submit'])) {
                                $allowedExts = array("gif", "jpeg", "jpg", "png");
                                $temp = explode(".", $_FILES["file"]["name"]);  //ten hinh va duoi file
                                $extension = end($temp); //hien cai duoi
                                if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && in_array($extension, $allowedExts)) {
                                    if ($_FILES["file"]["error"] > 0) {
                                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                                    } else {
                                     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                                        echo "Type: " . $_FILES["file"]["type"] . "<br>";
                                       echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                                       echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                                        $file_name = $temp[0] . '-' . time() . '.' . $temp[1]; //upload cung luc ko loi
                                        if (file_exists("public/image/" . $_FILES["file"]["name"])) {
                                            echo $_FILES["file"]["name"] . " already exists. ";
                                        } else {
                                            move_uploaded_file($_FILES["file"]["tmp_name"], "public/image/" . $file_name);
                                         echo "Stored in: " . "public/image/" . $file_name . "</br>";
                                        }
                                        $array = array(
                                            "id" => $_GET["id"],
                                            "name" => $_POST["name"],
                                            "location" => $_POST["location"],
                                            "company_address" => $_POST["company_address"],
                                            "image" => "public/image/" . $file_name
                                        );
                                        $keyword->update($array);
                                        $url = "public/image/" . $file_name;
                                    }
                                } else {
                                    echo "Invalid file";
                                }
                                echo "<img src='$url ' width='150px'>";
                            }
                            ?>
                        </div> 
    
                <footer class="footer">
                    <tr><td>TRƯỜNG CAO ĐẲNG CÔNG NGHỆ THỦ ĐỨC</td></tr><br>
                        <tr> <td></td><td>Designed by C12TDT Web2</td></tr>
             
                   
                </footer>
        
    </body>
</html>
